﻿namespace Smart.Utils.UI.BarcodeLib
{
    using System;

    internal class MSI : BarcodeCommon, IBarcode
    {
        private BarCodeStyles _encodedBarCodeStyles = BarCodeStyles.UNSPECIFIED;
        private string[] MSI_Code = new string[] { "100100100100", "100100100110", "100100110100", "100100110110", "100110100100", "100110100110", "100110110100", "100110110110", "110100100100", "110100100110" };

        public MSI(string input, BarCodeStyles encodedBarCodeStyles)
        {
            this._encodedBarCodeStyles = encodedBarCodeStyles;
            base.Raw_Data = input;
        }

        private string Encode_MSI()
        {
            string str2;
            string str3;
            int num;
            int num2;
            int num3;
            int num4;
            char ch2;
            if (!BarcodeEdit.CheckNumericOnly(base.Raw_Data))
            {
                throw new Exception("EMSI-1: Numeric Data Only");
            }
            string str = base.Raw_Data;
            if ((this._encodedBarCodeStyles == BarCodeStyles.MSI_Mod10) || (this._encodedBarCodeStyles == BarCodeStyles.MSI_2Mod10))
            {
                str2 = "";
                str3 = "";
                for (num = str.Length - 1; num >= 0; num -= 2)
                {
                    ch2 = str[num];
                    str2 = ch2.ToString() + str2;
                    if ((num - 1) >= 0)
                    {
                        ch2 = str[num - 1];
                        str3 = ch2.ToString() + str3;
                    }
                }
                str2 = Convert.ToString((int) (int.Parse(str2) * 2));
                num2 = 0;
                num3 = 0;
                foreach (char ch in str3)
                {
                    num2 += int.Parse(ch.ToString());
                }
                foreach (char ch in str2)
                {
                    num3 += int.Parse(ch.ToString());
                }
                num4 = 10 - ((num3 + num2) % 10);
                str = str + num4.ToString();
            }
            if ((this._encodedBarCodeStyles == BarCodeStyles.MSI_Mod11) || (this._encodedBarCodeStyles == BarCodeStyles.MSI_Mod11_Mod10))
            {
                int num5 = 0;
                int num6 = 2;
                for (num = str.Length - 1; num >= 0; num--)
                {
                    if (num6 > 7)
                    {
                        num6 = 2;
                    }
                    ch2 = str[num];
                    num5 += int.Parse(ch2.ToString()) * num6++;
                }
                num4 = 11 - (num5 % 11);
                str = str + num4.ToString();
            }
            if ((this._encodedBarCodeStyles == BarCodeStyles.MSI_2Mod10) || (this._encodedBarCodeStyles == BarCodeStyles.MSI_Mod11_Mod10))
            {
                str2 = "";
                str3 = "";
                for (num = str.Length - 1; num >= 0; num -= 2)
                {
                    ch2 = str[num];
                    str2 = ch2.ToString() + str2;
                    if ((num - 1) >= 0)
                    {
                        str3 = str[num - 1].ToString() + str3;
                    }
                }
                str2 = Convert.ToString((int) (int.Parse(str2) * 2));
                num2 = 0;
                num3 = 0;
                foreach (char ch in str3)
                {
                    num2 += int.Parse(ch.ToString());
                }
                foreach (char ch in str2)
                {
                    num3 += int.Parse(ch.ToString());
                }
                str = str + ((10 - ((num3 + num2) % 10))).ToString();
            }
            string str4 = "110";
            foreach (char ch in str)
            {
                str4 = str4 + this.MSI_Code[int.Parse(ch.ToString())];
            }
            return (str4 + "1001");
        }

        public string Encoded_Value
        {
            get
            {
                return this.Encode_MSI();
            }
        }
    }
}

